/*
*  Copyright 2019-2020 Zheng Jie
*
*  Licensed under the Apache License, Version 2.0 (the "License");
*  you may not use this file except in compliance with the License.
*  You may obtain a copy of the License at
*
*  http://www.apache.org/licenses/LICENSE-2.0
*
*  Unless required by applicable law or agreed to in writing, software
*  distributed under the License is distributed on an "AS IS" BASIS,
*  WITHOUT WARRANTIES OR CONDITIONS OF ANY KIND, either express or implied.
*  See the License for the specific language governing permissions and
*  limitations under the License.
*/
package com.yuecai.modules.yc_user.service.impl;

import com.yuecai.modules.yc_user.domain.YcUserWechat;
import com.yuecai.modules.yc_user.repository.YcUserWechatRepository;
import com.yuecai.modules.yc_user.service.YcUserWechatService;
import com.yuecai.modules.yc_user.service.dto.YcUserWechatDto;
import com.yuecai.modules.yc_user.service.dto.YcUserWechatQueryCriteria;
import com.yuecai.modules.yc_user.service.mapstruct.YcUserWechatMapper;
import com.yuecai.utils.FileUtil;
import com.yuecai.utils.PageUtil;
import com.yuecai.utils.QueryHelp;
import com.yuecai.utils.ValidationUtil;
import lombok.RequiredArgsConstructor;
import org.springframework.data.domain.Page;
import org.springframework.data.domain.Pageable;
import org.springframework.stereotype.Service;
import org.springframework.transaction.annotation.Transactional;

import javax.servlet.http.HttpServletResponse;
import java.io.IOException;
import java.util.ArrayList;
import java.util.LinkedHashMap;
import java.util.List;
import java.util.Map;

/**
* @website https://el-admin.vip
* @description 服务实现
* @author dm
* @date 2020-10-24
**/
@Service
@RequiredArgsConstructor
public class YcUserWechatServiceImpl implements YcUserWechatService {

    private final YcUserWechatRepository ycUserWechatRepository;
    private final YcUserWechatMapper ycUserWechatMapper;

    @Override
    public Map<String,Object> queryAll(YcUserWechatQueryCriteria criteria, Pageable pageable){
        Page<YcUserWechat> page = ycUserWechatRepository.findAll((root, criteriaQuery, criteriaBuilder) -> QueryHelp.getPredicate(root,criteria,criteriaBuilder),pageable);
        return PageUtil.toPage(page.map(ycUserWechatMapper::toDto));
    }

    @Override
    public List<YcUserWechatDto> queryAll(YcUserWechatQueryCriteria criteria){
        return ycUserWechatMapper.toDto(ycUserWechatRepository.findAll((root, criteriaQuery, criteriaBuilder) -> QueryHelp.getPredicate(root,criteria,criteriaBuilder)));
    }

    @Override
    @Transactional
    public YcUserWechatDto findById(Long id) {
        YcUserWechat ycUserWechat = ycUserWechatRepository.findById(id).orElseGet(YcUserWechat::new);
        ValidationUtil.isNull(ycUserWechat.getId(),"YcUserWechat","id",id);
        return ycUserWechatMapper.toDto(ycUserWechat);
    }

    @Override
    @Transactional(rollbackFor = Exception.class)
    public YcUserWechatDto create(YcUserWechat resources) {
        return ycUserWechatMapper.toDto(ycUserWechatRepository.save(resources));
    }

    @Override
    @Transactional(rollbackFor = Exception.class)
    public void update(YcUserWechat resources) {
        YcUserWechat ycUserWechat = ycUserWechatRepository.findById(resources.getId()).orElseGet(YcUserWechat::new);
        ValidationUtil.isNull( ycUserWechat.getId(),"YcUserWechat","id",resources.getId());
        ycUserWechat.copy(resources);
        ycUserWechatRepository.save(ycUserWechat);
    }

    @Override
    public void deleteAll(Long[] ids) {
        for (Long id : ids) {
            ycUserWechatRepository.deleteById(id);
        }
    }

    @Override
    public void download(List<YcUserWechatDto> all, HttpServletResponse response) throws IOException {
        List<Map<String, Object>> list = new ArrayList<>();
        for (YcUserWechatDto ycUserWechat : all) {
            Map<String,Object> map = new LinkedHashMap<>();
            map.put(" userId",  ycUserWechat.getUserId());
            map.put("userID ", ycUserWechat.getOpenId());
            map.put(" nickName",  ycUserWechat.getNickName());
            map.put(" gender",  ycUserWechat.getGender());
            map.put(" avatarUrl",  ycUserWechat.getAvatarUrl());
            map.put(" unionId",  ycUserWechat.getUnionId());
            map.put(" country",  ycUserWechat.getCountry());
            map.put(" province",  ycUserWechat.getProvince());
            map.put(" city",  ycUserWechat.getCity());
            map.put("创建时间", ycUserWechat.getCreateTime());
            list.add(map);
        }
        FileUtil.downloadExcel(list, response);
    }

    @Override
    public YcUserWechat findByOpenId(String openId) {
        return ycUserWechatRepository.findByOpenId(openId);
    }

}